package flc.ast.dialog;

import static com.blankj.utilcode.util.StringUtils.getString;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;

import flc.ast.activity.IconReplaceActivity;
import flc.ast.databinding.DialogIconBinding;
import flc.ast.fragment.IconSelTabFragment;
import stark.common.basic.base.BaseSmartDialog;

public class IconSelDialog extends DialogFragment {

    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitle = new String[]{};
    private TabLayout tlIcon;
    private ViewPager vpIcon;
    private ImageView cancel,confirm;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL,R.style.DialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_icon,container);
        tlIcon=view.findViewById(R.id.tlIcon);
        vpIcon=view.findViewById(R.id.vpIcon);
        vpIcon.setSaveEnabled(false);
        cancel=view.findViewById(R.id.ivDialogIconCancel);
        confirm=view.findViewById(R.id.ivDialogIconConfirm);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ilistener.clickCancel();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ilistener.clickConfirm();
            }
        });
        tabTitle = new String[]{getString(R.string.icon_type11), getString(R.string.icon_type22), getString(R.string.icon_type33), getString(R.string.icon_type44), getString(R.string.icon_type55), getString(R.string.icon_type66)};
        setTabData();
        return view;
    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        //避免重复添加的异常 java.lang.IllegalStateException: Fragment already added
//        Fragment fragment = manager.findFragmentByTag(tag);
//        if (fragment != null) {
//            FragmentTransaction fragmentTransaction = manager.beginTransaction();
//            fragmentTransaction.remove(fragment);
//            fragmentTransaction.commitAllowingStateLoss();
//        }
        //避免状态丢失的异常 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
//        try {
//            super.show(manager, tag);
//        } catch (IllegalStateException e) {
//            e.printStackTrace();
//        }
        try {
            if (manager == null || manager.isDestroyed()) {
                return;
            }
            manager.beginTransaction().remove(this).commit();
            super.show(manager, tag);
        }  catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    private void setTabData() {
        List<String> list = new ArrayList<>();
        list.add(AppConfig.ICON_DATA1);
        list.add(AppConfig.ICON_DATA2);
        list.add(AppConfig.ICON_DATA3);
        list.add(AppConfig.ICON_DATA4);
        list.add(AppConfig.ICON_DATA5);
        list.add(AppConfig.ICON_DATA6);
        for (int i = 0; i < list.size(); i++) {
            fragments.add(new IconSelTabFragment().newInstance(list.get(i)));
            tlIcon.addTab(tlIcon.newTab().setText(tabTitle[i]));
        }
        vpIcon.setAdapter(new IconSelDialog.ArticleAdapter(getChildFragmentManager()));
        tlIcon.setupWithViewPager(vpIcon);
        //给tab每个item设置值
        for (int i = 0; i < tlIcon.getTabCount(); i++) {
            TabLayout.Tab tab = tlIcon.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = tlIcon.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvTab);
        ImageView iv = view.findViewById(R.id.ivTab);
        iv.setVisibility(View.VISIBLE);
        tv.setTextSize(18);
        //设置TabLayout点击事件
        tlIcon.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                ImageView iv = view.findViewById(R.id.ivTab);
                iv.setVisibility(View.VISIBLE);
                tv.setTextSize(18);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvTab);
                ImageView iv = view.findViewById(R.id.ivTab);
                iv.setVisibility(View.GONE);
                tv.setTextSize(15);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class ArticleAdapter extends FragmentStatePagerAdapter {
        public ArticleAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_icon_tab, null);
        TextView text = view.findViewById(R.id.tvTab);
        ImageView img = view.findViewById(R.id.ivTab);
        text.setText(tabTitle[i]);
        text.setTextSize(15);
        img.setVisibility(View.GONE);
        return view;
    }

    private Ilistener ilistener;

    public void setIlistener(Ilistener ilistener) {
        this.ilistener = ilistener;
    }

    public interface Ilistener {
        void clickCancel();

        void clickConfirm();
    }

}
